const express=require("express");
const db=require("./model/db.js");

var token=require("./model/token.js");
const got =require("got")

const app=express()

// var result = db.add("test",{age:21}).then(res=>{
// 	// console.log(res,"1111")
// }).catch(err=>{
// 	console.error(err)
// })

app.get("/api/send",async (req,res)=>{
	// console.log(token,"111")
	var tokenStr= await token
	// console.log(tokenStr)
	var context={
		"touser": "oEH7Y5e7ygJuzaxfXbtzCIy-VGgc",
		"template_id": "-swmuIHPkg9ir2rFyX81jav6W0N0a6rEV3lQcWBR7Dk",
		"page": "pages/index/index",
		"miniprogram_state":"developer",
		"lang":"zh_CN",
		"data": {
			"number01": {
				"value": "339208499"
			},
			"date01": {
				"value": "2015年01月05日"
			},
			"site01": {
				"value": "TIT创意园"
			} ,
			"site02": {
				"value": "广州市新港中路397号"
			}
		}
	}

	/*var context={
		"touser":"oEH7Y5e7ygJuzaxfXbtzCIy-VGgc",
		"weapp_template_msg":{
			"template_id":"-swmuIHPkg9ir2rFyX81jav6W0N0a6rEV3lQcWBR7Dk",
			"page":"page/page/index",
			"form_id":"FORMID",
			"data":{
				"keyword1":{
					"value":"339208499"
				},
				"keyword2":{
					"value":"2015年01月05日 12:30"
				},
				"keyword3":{
					"value":"腾讯微信总部"
				},
				"keyword4":{
					"value":"广州市海珠区新港中路397号"
				}
			},
			"emphasis_keyword":"keyword1.DATA"
		},
	}*/

	var url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+tokenStr;
	var result =await got.post(url,{json:context}).catch(err=>{
		console.error(err)
	})	
	console.log(result)

})

app.listen(3001,()=>{
	console.log("--3001--")
})